package com.web.graduationpro.util;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {
	private static PageUtil pageUtil;
	
	public PageUtil(){}
	
	public static PageUtil getInstance() {
        if (pageUtil == null) {
        	pageUtil = new PageUtil();
        }
        return pageUtil;
	}
	
	/**
	 * 分页
	 * @param  actionUrl 访问路径
	 * @param  totalNum  总条数
	 * @showNum 显示多少页
	 * @curPage  当前页
	 * @pageNum  每页多少条
	 * @return List 分页结果
	 * */
   public static List<String>  getPage(String actionUrl,int totalNum,int showNum,int curPage,int pageNum){
	    List<String> list = new ArrayList<String>();
	   
	    int start =  ((curPage-1)/showNum)*showNum+1;
	    int end  = start+showNum;
	    int allPage = (totalNum+pageNum-1)/pageNum;
		int currPage = 1;
		if(curPage>0){
			currPage = curPage;
		}
		
		String lastUrl=actionUrl+"currPage="+(curPage-1)+"&"+"pageSize="+pageNum;
	
		list.add("<span>共"+totalNum+"页  页次"+currPage+"/"+allPage+"页 </span> ");
		if(end>allPage){
			end=(allPage+1);
		}
		if(curPage<=1){
			list.add("<span class='nolink'>上一页</span>");
		}else{
			list.add("<a href='"+lastUrl+"'>上一页</a>");
			
		}
		if(allPage>0){
			if(end==start){	
				end =start+1;
			}
			for(long i=start;i<end;i++){
				String url = actionUrl+"currPage="+i+"&"+"pageSize="+pageNum;
				boolean cur=false;
				if(i==curPage){
					cur =true;
				}
				if(cur){
					list.add("<span><font color='red'>"+i+"</font></span>");
				}else{
					list.add("<a href='"+url+"' >"+i+"</a>");
				}
				if(i>=totalNum){
					break;
				}
			
			}
		}
		String nextUrl=actionUrl+"currPage="+(curPage+1)+"&"+"pageSize="+pageNum;
		if(curPage>=allPage){
			list.add("<span class='nolink'>下一页</span>");
		}else{
			list.add("<a href='"+nextUrl+"'>下一页</a>");
		}	
		if(allPage<2){
			list = null;
		}
		return list;
	}
   
}
